package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：gbc
 * @Project：sky-take-out
 * @Date：2025/9/24 16:06
 */
@Api(tags = "分类相关接口")
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @ApiOperation("分类查询")
    @GetMapping("/page")
    public Result<PageResult> page(String name, String page, String pageSize, String type) {
        CategoryPageQueryDTO categoryPageQueryDTO = new CategoryPageQueryDTO();

        Integer pageQuery = Integer.valueOf(page);
        Integer pageSizeQuery = Integer.valueOf(pageSize);
        if (type != null) {
            Integer typeQuery = Integer.valueOf(type);
            categoryPageQueryDTO.setType(typeQuery);
        }


        categoryPageQueryDTO.setName(name);
        categoryPageQueryDTO.setPage(pageQuery);
        categoryPageQueryDTO.setPageSize(pageSizeQuery);

        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 分类新增
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("分类新增")
    @PostMapping
    public Result saveCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable String status, String id) {
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 删除分类,如果分类下有未停售的菜品或套餐不允许删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping
    public Result delete(String id) {
        categoryService.delete(Long.valueOf(id));
        return Result.success();
    }

    /**
     * 根据分类类型查询分类
     * @return
     */
    @ApiOperation("根据分类类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> getCategoryByType(String type){
        return Result.success(categoryService.getCategoryByType(Integer.valueOf(type)));
    }
}
